package model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 
 * @author Sonny Sandberg
 */
public class User 
{
    private int 
            id,
            accessLevel;
    private String
            username, 
            password,
            email,
            securitykey;
    private ArrayList<ActionListener> listeners;

    public User(int id, String username, String password, String email, String securitykey, int accessLevel)
    {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
        this.securitykey = securitykey;
        this.accessLevel = accessLevel;
        listeners = new ArrayList();
        notifyListeners();
    }
    
    public User()
    {   
        listeners = new ArrayList();
        reset();
    }
    
    public void reset()
    {
        id = 0;
        username = "";
        password = "";
        email = "";
        securitykey = utility.Security.createRandomString(255);
        accessLevel = 1;
        notifyListeners();
    }

    public int getId()
    {
        return id;
    }

    public String getUsername()
    {
        return username;
    }

    public String getPassword()
    {
        return password;
    }

    public String getEmail()
    {
        return email;
    }

    public String getSecuritykey()
    {
        return securitykey;
    }

    public int getAccessLevel()
    {
        return accessLevel;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public void setAccessLevel(int accessLevel)
    {
        this.accessLevel = accessLevel;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public void setPassword(String password)
    {
        this.password = utility.Security.encryptString(password);
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public void setSecuritykey(String securitykey)
    {
        this.securitykey = securitykey;
    }
    
    public boolean isCompleted()
    {
        boolean answer = false;
        
        if (username.length() > 0 && password.length() > 0 && email.length() > 0 && accessLevel > 0 && securitykey.length() > 0)
        {
            answer = true;
        }
        
        return answer;
    }
    
    public void addListener(ActionListener listener) 
    {
        listeners.add(listener);
    }

    private void notifyListeners() 
    {
        ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Something changed");
        for (ActionListener listener : listeners) 
        {
            listener.actionPerformed(event);
        }
    }
}